<?php

/**
 * This is the model class for table "tbproducto".
 *
 * The followings are the available columns in table 'tbproducto':
 * @property string $Pr_idproducto_PK
 * @property string $Pr_descripcion
 * @property string $Pr_categoria
 * @property string $Pr_unidadmedida
 * @property string $Pr_ubicacion
 * @property string $Pr_fechaingreso
 * @property string $Pr_numconcurso
 * @property string $Pr_casacomercial
 * @property string $Pro_idproveedor_FK
 * @property string $Pr_precio
 * @property integer $Pr_cantidadminima
 *
 * The followings are the available model relations:
 * @property Tbbodegaproducto[] $tbbodegaproductos
 * @property Tbproveedor $proIdproveedorFK
 */
class catalogo extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbproducto';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Pr_idproducto_PK, Pr_descripcion, Pr_categoria, Pr_unidadmedida, Pr_numconcurso, Pro_idproveedor_FK, Pr_precio, Pr_cantidadminima', 'required'),
			array('Pr_cantidadminima', 'numerical', 'integerOnly'=>true),
			array('Pr_idproducto_PK', 'length', 'max'=>25),
			array('Pr_descripcion', 'length', 'max'=>500),
			array('Pr_categoria', 'length', 'max'=>2),
			array('Pr_unidadmedida', 'length', 'max'=>5),
			/*array('Pr_numconcurso, Pr_casacomercial', 'length', 'max'=>20),*/
			array('Pro_idproveedor_FK', 'length', 'max'=>15),
			array('Pr_precio', 'length', 'max'=>12),
			//Validacion agregada el dia 05/02/2014. para hacer el campo unico.
			array('Pr_idproducto_PK','unique','attributeName'=>'Pr_idproducto_PK'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('Pr_idproducto_PK, Pr_descripcion, Pr_categoria, Pr_unidadmedida, Pr_numconcurso, Pro_idproveedor_FK, Pr_precio, Pr_cantidadminima','safe','on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbbodegaproductos' => array(self::HAS_MANY, 'Tbbodegaproducto', 'Pr_idproducto_FK'),
			'proIdproveedorFK' => array(self::BELONGS_TO, 'Tbproveedor', 'Pro_idproveedor_FK'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Pr_idproducto_PK'=> 'Código',
			'Pr_descripcion'=> 'Descripción',
			'Pr_categoria'=> 'Categoría',
			'Pr_unidadmedida'=> 'Unidad de medida',
			'Pr_numconcurso'=> 'Concurso',
			'Pro_idproveedor_FK'=> 'Código proveedor',
			'Pr_precio'=> 'Precio',
			'Pr_cantidadminima'=> 'Cantidad mínima',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Pr_idproducto_PK',$this->Pr_idproducto_PK,true);
		$criteria->compare('Pr_descripcion',$this->Pr_descripcion,true);
		$criteria->compare('Pr_categoria',$this->Pr_categoria,true);
		$criteria->compare('Pr_unidadmedida',$this->Pr_unidadmedida,true);
		$criteria->compare('Pr_numconcurso',$this->Pr_numconcurso,true);
		$criteria->compare('Pro_idproveedor_FK',$this->Pro_idproveedor_FK,true);
		$criteria->compare('Pr_precio',$this->Pr_precio,true);
		$criteria->compare('Pr_cantidadminima',$this->Pr_cantidadminima);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination' => array(  'pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']))
		));
	}

	function toList()
	{
		$model=self::model()->findAll();
		return CHtml::listData($model, 'Pr_idproducto_PK', 'Pr_descripcion');
	}

/// funciones agregadas el 06-02-2014 para formatear la fecha del cjuidatepicker
		// public function beforeSave()
		// {
		// 	if(parent::beforeSave())
		// 	{

		// 	$this->Pr_fechaingreso= Yii::app()->dateformatter->format("yyyy-MM-dd",$this->Pr_fechaingreso);
		// 	parent::beforeSave();

		// 	return true;
		// }
		// 	else return false;
		// }

		
		// public function afterFind()
		// {
		// 		$this->Pr_fechaingreso= Yii::app()->dateformatter->format("dd-MM-yyyy",$this->Pr_fechaingreso);
		// 		parent::afterFind();
		// }

	public  function GetCategoria()
	{
		/* return  CHtml::listData(catalogo::model()->findAll(), "id","Pr_categoria");*/

		return array('Z'=>'Z','A'=>'A');	
	}


	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return catalogo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
